clear all
cd "yourpath/Figure3"
* set API key to retrieve NBER recession data. The key is personal; to obtain yours, go here: https://fred.stlouisfed.org/docs/api/api_key.html
set fredkey XXX, permanently 
* graph set window fontface "LM Roman 10"
graph set window fontface "Bookman Old Style"


/* Import recession data 
import fred USRECM
gen month = mofd(daten)
tsset month, monthly
label variable USRECM "Recession"

save recession, replace
clear all */

* Import data 
use "aggregatePlusCategories", clear
keep if category == "aggregate"
gen idx_Sref_v1_r_SA = sum(DL_Sref_v1_r_SA)
local vars "idx_price_raw_v1_r_SA idx_price_ref_v1_r_SA idx_Sref_v1_r_SA ext_CPI_FAH_SA_inf"
keep `vars' month
tsset month

insobs 1, before(1)
replace month = tm(2001m1) in 1
sort month

global vars_yoy ""
foreach var of local vars {
	replace `var' = 0 in 1
	gen `var'_exp = exp(`var')
	gen `var'_yoy = log(`var'_exp/L12.`var'_exp)
	global vars_yoy = "${vars_yoy} `var'_yoy"
}

* Create yoy
label var idx_price_raw_v1_r_SA_yoy "Posted-price YoY change"
label var idx_price_ref_v1_r_SA_yoy "Reference-price YoY change"
label var idx_Sref_v1_r_SA_yoy "Sales-price YoY change"
label var ext_CPI_FAH_SA_inf_yoy "CPI Food-at-home YoY change"

* Express in %
global vars_yoy_percent ""
foreach var of global vars_yoy {
	gen `var'percent = `var'*100
	global vars_yoy_percent "${vars_yoy_percent} `var'percent"
}

label var idx_price_raw_v1_r_SA_yoypercent "Posted-price YoY change in %"
label var idx_price_ref_v1_r_SA_yoypercent "Reference-price YoY change in %"
label var idx_Sref_v1_r_SA_yoypercent "Sales-price YoY change in %"
label var ext_CPI_FAH_SA_inf_yoypercent "CPI Food-at-home YoY change in %"

* Merge data with recession
merge 1:1 month using recession.dta, keepusing(USRECM) keep(match) nogenerate

summarize idx_price_raw_v1_r_SA_yoy idx_price_ref_v1_r_SA_yoy idx_Sref_v1_r_SA_yoy		ext_CPI_FAH_SA_inf_yoy
summarize idx_price_raw_v1_r_SA_yoypercent idx_price_ref_v1_r_SA_yoypercent idx_Sref_v1_r_SA_yoypercent ext_CPI_FAH_SA_inf_yoypercent

gen USRECM_max = 8 if USRECM == 1 
gen USRECM_min = -4 if USRECM == 1

drop if month < tm(2002m1)

* Graph
#delimit ;
	twoway (area USRECM_max USRECM_min month if inrange(month, tm(2001m1), tm(2011m12)), color(gs15 gs15) )
	(line ${vars_yoy_percent} month if inrange(month, tm(2001m1), tm(2011m12)),
	yaxis(1 2)
	lpattern(dash solid longdash solid)  lcolor(gs5 gs0 gs9 gs13)
	graphregion(color(white)) plotregion(margin(zero))
	legend(cols(2) order(3 "Posted-price inflation" 4 "Reference-price inflation" 5 "Sales-price inflation" 6 "CPI Food-at-home inflation") region(lstyle(none)))
	xtitle("") xscale() xlabel(504(12)624, format(%tmCY))
	yscale(r(-3.5 8)) ylabel(-2(2)8, angle(0))
	ytitle("%", axis(2) angle(0) orient(horizontal))
	ytitle("%", axis(1) angle(0) orient(horizontal))
	ylabel(-2(2)8, axis(2) angle(0))) ;
#delimit cr 

graph export ".\Figure3.eps", replace 
graph export ".\Figure3.png", replace width(1600)
graph export ".\Figure3.pdf", replace
